import request from '@/utils/request'

// 获取商品列表
export function getProducts(params) {
  return request({
    url: '/api/products/search',
    method: 'get',
    params
  })
}

// 获取商品详情
export function getProduct(id) {
  return request({
    url: `/api/products/${id}`,
    method: 'get'
  })
}

// 获取商品分类
export function getCategories() {
  return request({
    url: '/api/products/categories',
    method: 'get'
  })
}

// 获取子分类
export function getSubCategories(parentId) {
  return request({
    url: `/api/products/categories/${parentId}/subcategories`,
    method: 'get'
  })
}

// 获取商品折扣信息
export function getProductDiscountInfo(productId, userCreditScore) {
  return request({
    url: `/api/products/${productId}/discount-info`,
    method: 'get',
    params: { userCreditScore }
  })
}

// 检查分期付款资格
export function checkInstallmentEligibility(productId) {
  return request({
    url: `/api/products/${productId}/installment-eligibility`,
    method: 'get'
  })
}

// 创建订单
export function createOrder(data) {
  return request({
    url: '/api/products/orders',
    method: 'post',
    data
  })
}

// 获取订单详情
export function getOrder(id) {
  return request({
    url: `/api/products/orders/${id}`,
    method: 'get'
  })
}

// 获取用户订单列表
export function getUserOrders(userId, params) {
  return request({
    url: `/api/products/orders/user/${userId}`,
    method: 'get',
    params
  })
}

// 更新订单状态
export function updateOrderStatus(id, status) {
  return request({
    url: `/api/products/orders/${id}/status`,
    method: 'put',
    params: { status }
  })
}

// 更新支付状态
export function updatePaymentStatus(id, status) {
  return request({
    url: `/api/products/orders/${id}/payment-status`,
    method: 'put',
    params: { status }
  })
}

// 获取推荐商品
export function getRecommendedProducts(userId, limit = 10) {
  return request({
    url: '/api/products/recommendations/user-based',
    method: 'get',
    params: { userId, limit }
  })
}

// 记录商品浏览
export function recordProductView(productId, userId, viewDuration) {
  return request({
    url: `/api/products/${productId}/view`,
    method: 'post',
    params: { userId, viewDuration }
  })
}

// 记录商品点击
export function recordProductClick(productId, userId) {
  return request({
    url: `/api/products/${productId}/click`,
    method: 'post',
    params: { userId }
  })
}

// 记录商品购买
export function recordProductPurchase(productId, userId) {
  return request({
    url: `/api/products/${productId}/purchase`,
    method: 'post',
    params: { userId }
  })
} 